package com.devfest.wishmaster.gui;

import java.text.SimpleDateFormat;
import java.util.List;

import com.devfest.wishmaster.R;
import com.devfest.wishmaster.model.WishList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class WishListAdapter extends BaseAdapter {

	private List<WishList> wishLists;
	private LayoutInflater mInflater;
	Context context;

	public WishListAdapter(Context context, List<WishList> wishLists) {
		this.wishLists = wishLists;
		this.context = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		if (wishLists != null)
			return wishLists.size();
		return 0;
	}

	@Override
	public WishList getItem(int position) {
		if (position < 0 || position >= wishLists.size())
			return null;
		return wishLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		if (position < 0 || position >= wishLists.size())
			return -1;
		return wishLists.get(position).getId();
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.activity_wish_list_item,
					null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView
					.findViewById(R.id.wish_list_item_name);
			holder.comment = (TextView) convertView
					.findViewById(R.id.wish_list_item_comment);
			holder.eventDate = (TextView) convertView
					.findViewById(R.id.wish_list_item_event_date);
			holder.item = (TextView) convertView
					.findViewById(R.id.wish_list_item_list);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		WishList wishList = wishLists.get(position);
		holder.name.setText(wishList.getName());
		holder.comment.setText(wishLists.get(position).getNewCommentCount()
				+ "/" + wishLists.get(position).getCommentCount());
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		holder.eventDate.setText(formatter.format(wishList.getEventDate()));
		holder.item.setText(wishList.getWishItems().size() + "");
		return convertView;
	}

	private static class ViewHolder {
		TextView name;
		TextView eventDate;
		TextView item;
		TextView comment;
	}
}